import axios from 'axios'

const instance = axios.create({
  baseURL: '/api',
  timeout: 60000
})

instance.interceptors.request.use(
  (config) => {
    config.headers['content-language'] = 'zh-cn'
    return config
  },
  (error) => {
    return Promise.reject(error)
  }
)

instance.interceptors.response.use(
  (response) => {
    const { data, status } = response
    if (status === 200) {
      if (data.code === 401) {
        window.location.reload()
        return null
      }
      return data
    } else {
      console.log()
    }
  },
  (error) => {
    return Promise.reject(error)
  }
)

export default instance
